﻿$(window).resize(function () {
    $("#content").css("height", $(".layui-body").height() - 75);
});

$(function () {
    //权限全选
    $("input[name='checkAll']").click(function () {
        if ($(this).prop("checked") == true) {

            $(this).parent().parent().siblings("td").find("div").find("input[type='checkbox']").prop("checked", true);
        } else {
            $(this).parent().parent().siblings("td").find("div").find("input[type='checkbox']").prop("checked", false);
        }
    });
    $("#IndexId", window.parent.document).css("background-color", "white");
    $("#content").css("height", $(".layui-body").height() - 75);
});
//返回
$("#btn_Return").click(function () {
    window.location.href = '/Role/RoleIndex';
});
layui.config({
    base: "../Content/layui/lay/modules/"
}).use(['element', 'jquery', 'multiSelect', 'form', 'layedit', 'laydate'], function () {
    var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    //监听导航点击
    element.on('nav(demo)', function (elem) {
        //console.log(elem)
        layer.msg(elem.text());
    });

    function CheckBoxInit() {
        $(".layui-form-checkbox").each(function () {
            $(this).css("margin-left", "100px");
        }) //style = "font-family:"Microsoft YaHei";font-size:16"
        $(".layui-form-checkbox span").each(function () {
            $(this).css("font-family", "Microsoft YaHei");
            $(this).css("font-size", "16px");
        })
    }

    var rightlist = [];
    var active = {
        close: function (data) {
            //layer.close(layer.index);
        }
    };
    var form = layui.form, layer = layui.layer;

    function checkall(obj) {
        var id = obj.id;
        $("input[name='" + id + "']").each(function () {
            $(this).checked = true;
        })
    }

    function getrightobj(list, right, type) {
        var hasright = false;
        list.forEach(v => {
            if (v.MenuId === right.MenuId) {
                getright(v, type);
                hasright = true;
                return;
            }
        });
        if (!hasright) {
            getright(right, type);
            list.push(right);
        }
    }

    function getright(right, type) {
        if (type == "add") {
            right.IsCanAdd = 1;
        }
        if (type == "view") {
            right.IsCanView = 1;
        }
        if (type == "list" || type =="export") {
            right.IsCanView = 1;
        }
        if (type == "update") {
            right.IsCanUpdate = 1;
        }
        if (type == "delete") {
            right.IsCanDelete = 1;
        }
    }
    form.on("checkbox(checkallbytype)", function (data) {
        var type = data.elem.dataset.type;
        $("input[type='checkbox'][data-type='" + type + "']").each(function () {
            if (!this.checked == data.elem.checked) {
                $(this).click();
            }
        });
        form.render();
    })

    //监听提交
    form.on('submit(Add)', function (data) {
        var rightlist = []
        $("input:checked").each(function () {
            var inputname = this.name;
            if (!inputname) {
                return;
            }
            var right = {}
            right.MenuId = $("#" + inputname).data("id");
            getrightobj(rightlist, right, $(this).data("type"));
        })
        $.post("./SaveRole", { Role: data.field, right: rightlist }, function (result) {
            layer.alert(result.Message, { title: "保存结果" }, function (index) {
                layer.close(index);
                location.href = "./RoleIndex";
                //window.parent.jQuery("#layAddRole")[0].lastChild.click();
            });
        })
        return false;
    });
    //$.post("../../Sys/GetAllMenu", function (result) {
    //    if (result.length > 0) {
    //        debugger;
    //        $(".MenuList").empty();
    //        //Showcheckall();
    //        for (var i = 0; i < result.length; i++) {
    //            ShowRight(result[i]);
    //        }
    //        form.render();
    //        CheckBoxInit();

    //    }
    //});

});
